package com.wgz.ai.langchain4jai;

import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.data.message.ImageContent;
import dev.langchain4j.data.message.TextContent;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.model.output.Response;

/**
 * @author WGZ
 * @date 2025/3/4 14:17
 * OpenAI 开始
 */
public class HelloWorld {
    private final static String apiKey = "sk-svcacct-J-zoI616zoIN8ghM6amdG_SbFNc3vADd5yRa-53EQkr-lPN0tjD8QgWJfAmSuT3BlbkFJxBhbhopzngM8ZUB8KwByRFPnJ5bDUT1eq2Kop0OlggeqfzdsNSF45DQFZ94mgA";
    public static void main(String[] args) {
        multiRoundDialogue();
        imgDialogue();
    }

    /**
     * 开始
     */
    public void start(){
        OpenAiChatModel model = OpenAiChatModel.builder()
                .apiKey("")
                .modelName("gpt-3.5-turbo")
                .temperature(0.3)
                .build();

        String response = model.generate("用一句话解释量子计算");
        System.out.println(response);
    }

    /**
     * 多轮对话的管理
     */
    public static void multiRoundDialogue(){
        OpenAiChatModel model = OpenAiChatModel.builder()
                .apiKey(apiKey)
                .modelName("gpt-3.5-turbo")
                .temperature(0.3)
                .build();

        //多轮对话的管理
        UserMessage firstUserMessage = UserMessage.from("你好，我叫万光宗");
        System.out.println(firstUserMessage);
        AiMessage firstAiMessage = model.generate(firstUserMessage).content();
        System.out.println(firstAiMessage.text());
        UserMessage secondUserMessage = UserMessage.from("我的名字是什么？");
        AiMessage secondAiMessage = model.generate(firstUserMessage, firstAiMessage, secondUserMessage).content();
        System.out.println(secondAiMessage.text());
    }


    /**
     * 多模态图片识别
     */
    public static void imgDialogue(){
        OpenAiChatModel model = OpenAiChatModel.builder()
                .apiKey(apiKey)
                .modelName("gpt-3.5-turbo")
                .temperature(0.3)
                .build();


        UserMessage userMessage = UserMessage.from(
                TextContent.from("描述下面的图片:")
//                ImageContent.from("data:image/png;base64,"+UrlToBase64Util.imageUrlToBase64("https://d2731bbzmt3wpb.cloudfront.net/userHeadImg/us-west-2:0cd30497-4403-42ad-8421-1da962028589/03001f90c10c4c99a46cd2329c42b3ba.png"))
        );

        Response<AiMessage> generate = model.generate(userMessage);
        System.out.println(generate.content().text());
    }

}
